﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Controls;

namespace Eniux.Windows.Interactivity.Behaviors
{
    /// <summary>
    /// 操作确认行为
    /// </summary>
    public  class ConfirmBehavior : Behavior<UIElement>
    {
        #region 注册属性

        #region CommandParameter

        /// <summary>
        ///命令参数
        /// </summary>
        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ConfirmBehavior), null);

        #endregion

        #region Command

        /// <summary>
        /// 命令
        /// </summary>
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }
        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ConfirmBehavior), null);

        #endregion

        #region IsConfirm

        /// <summary>
        /// 是否确认
        /// </summary>
        public bool IsConfirm
        {
            get { return (bool)GetValue(IsConfirmProperty); }
            set { SetValue(IsConfirmProperty, value); }
        }
        public static readonly DependencyProperty IsConfirmProperty = DependencyProperty.Register("Confirm", typeof(bool), typeof(ConfirmBehavior), null);

        #endregion

        #region Title

        /// <summary>
        /// 确认消息标题
        /// </summary>
        public string Title
        {
            get { return (string)GetValue(ConfirmCaptionProperty); }
            set { SetValue(ConfirmCaptionProperty, value); }
        }
        public static readonly DependencyProperty ConfirmCaptionProperty = DependencyProperty.Register("Title", typeof(string), typeof(ConfirmBehavior), null);

        #endregion

        #region Message

        /// <summary>
        /// 要显示的消息
        /// </summary>
        public string Message
        {
            get { return (string)GetValue(ConfirmMessageProperty); }
            set { SetValue(ConfirmMessageProperty, value); }
        }
        public static readonly DependencyProperty ConfirmMessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(ConfirmBehavior), null);

        #endregion 

        #endregion

        #region 重写

        protected override void OnAttached()
        {
            base.OnAttached();
            if (AssociatedObject is ButtonBase)
            {
                ((ButtonBase)AssociatedObject).Click += new RoutedEventHandler(PromptAndExecuteCommand);
            }
            else
            {
                AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(PromptAndExecuteCommand);
            }

        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            ((Button)AssociatedObject).Click -= PromptAndExecuteCommand;
            AssociatedObject.MouseLeftButtonDown -= PromptAndExecuteCommand;
        }
        
        #endregion

        #region 方法

        void PromptAndExecuteCommand(object sender, RoutedEventArgs e)
        {
            if (!IsConfirm || MessageBoxResult.OK == MessageBox.Show(Message, Title, MessageBoxButton.OKCancel))
            {
                if (Command != null)
                {
                    Command.Execute(CommandParameter);
                }
            }
        } 
        #endregion
    }
}
